/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var stage1 = 
        "                              "+
        "           2                  "+
        "           2                  "+
        "22222222222222222222222222    "+
        "000000000000000               "+
        "00111112   2222222222222222222"+
        "00111112     00000001111111111"+
        "01111222222222222222 222222221"+
        "01111220             211111121"+
        "01111220 222222222222211121021"+
        "11102220              21211021"+
        "111022222222222222222 21121021"+
        "1222222               21121111"+
        " 2      2222222222111111122222"+
        " 22 222222222222222222222     "+
        "                              "+
        "222222222222222222222222      "+
        "                              "+
        "        2222222222222222222222"+
        " 000000000000000000000000    e";
var stage2 =
      //'______________________________"
        "                          2   "+
        "222222 22222222222222222222 2 "+
        "       2                  2 2 "+
        "2   2  2  22222222111112212 2e"+
        "2   2     21111111111112212 22"+
        "22  22222222112111112112212   "+
        "      111111112112222112212   "+
        "    22222222222111112112212   "+
        "    2                     2   "+
        "    2   222222222222222222    "+
        "11112 2        211       2    "+
        "11112 2 2222   2112  2   2    "+
        "11112 2 2      2112  2   222  "+
        "11112 2 2 222222112  2     2  "+
        "11112 2 2      2112  22222 2  "+
        "11112 2 2222   2112      2 2  "+
        "11112 2 2      2112      2    "+
        "11         2222211222222 2    "+
        "11  222222222222         2    "+
        "00000000000000000000000002    ";
var blockX = 40, blockY = 40, lines = 20, lineWidth = 30;



function Maze(scene, stage, exit)
{
    this.exit = exit;
    this.image = 
            [
                new Image(),
                new Image(),
                new Image(),
                new Image(),
                new Image()
            ];
    this.image[2].src = "img/wall40.png";
    this.image[1].src = "img/rock40.png";
    this.image[0].src = "img/grass40.png";
    this.scene = scene;
    
    this.Objects = createMap(stage);

    this.Draw = function(frameNumber)
    {
        this.scene.Context.fillStyle = 'rgba(255,255,0,1)';
        for (ob in this.Objects)
            {
                var actual = this.Objects[ob];
                var im = this.image[actual.i];
            this.scene.Context.drawImage(im, actual.x, actual.y);
            }
//    scene.fillRect(this.Objects[ob].x, this.Objects[ob].y, this.Objects[ob].w, this.Objects[ob].h);

        this.scene.Context.fillStyle = 'rgba(0,0,0,1)';
    };
    this.canMove = function(a, b, c, d) {
        
        var xs = a+(c-a)/2;
        var ys = b+(d-b)/2;
        var xe = this.exit.x+this.exit.offset;
        var ye = this.exit.y+this.exit.offset;
        var dd=(xe-xs)*(xe-xs)+(ye-ys)*(ye-ys);
        if(dd<250)
            StageStop(stage);
    /*
     //      b
     //     ___
     //    |   |
     //  a |___| c
     //      
     //      d
     
     
     */
        
        //var v = document.getElementById("stdOUT");
        //v.textContent = "a:"+a+"<br/>b"+b+"<br/>c"+c+"<br/>b"+d;
        var endvalue=(-1);
        for (ob in this.Objects)
            if (a < this.Objects[ob].x1)
                if (c > this.Objects[ob].x)
                    if (b < this.Objects[ob].y1)
                        if (d > this.Objects[ob].y)
                            if(this.Objects[ob].i>endvalue)
                                endvalue = this.Objects[ob].i;
        return endvalue;

    };

}
    function createMap (map)
    {
        
        Objects = new Array();
        for(var y = 0;y<lines;y++ )
            for(var x = 0 ;x<lineWidth;x++)
            {
            var i = parseInt(map[x+y*lineWidth] );
            if(!isNaN(i))
                Objects.push({
                    x: x * blockX,
                    y: y * blockY,
        //            w: this.arr[ob].w * blockX,
        //            h: this.arr[ob].h * blockY,
                    x1: x * blockX + blockX,
                    y1: y * blockY + blockY,
                    i: 0+(parseInt(map[x+y*lineWidth]))
                });
            }
        return Objects;            
    }